package com.jwdf.uda.ui.fragment.user.personal;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.databinding.FragmentOldPassChangeBinding;
import com.jwdf.uda.event.IndividualEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.UserChangePassowrdAction;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.ToastUtils;
import com.jwdf.uda.utils.UdaUtils;

import retrofit2.Response;

/**
 * Created by Administrator on 2016/7/8.
 */
public class OldPassChangeFragment extends BaseFragment {
    private FragmentOldPassChangeBinding mDataBinding;
    private View.OnFocusChangeListener mInputFocusListener = new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (R.id.input_edit_old_pass == v.getId()) {
                if (!hasFocus) {
                    mDataBinding.ivClearOldPassInput.setVisibility(View.GONE);
                } else {
                    if (mDataBinding.inputEditOldPass.getText().toString().length() > 0) {
                        mDataBinding.ivClearOldPassInput.setVisibility(View.VISIBLE);
                    }
                }
            } else if (R.id.input_edit_passwd == v.getId()) {
                if (!hasFocus) {
                    mDataBinding.ivClearPasswdInput.setVisibility(View.GONE);
                } else {
                    if (mDataBinding.inputEditPasswd.getText().toString().length() > 0) {
                        mDataBinding.ivClearPasswdInput.setVisibility(View.VISIBLE);
                    }
                }
            } else if (R.id.input_edit_confirm_passwd == v.getId()) {
                if (!hasFocus) {
                    mDataBinding.ivClearConfirmPasswdInput.setVisibility(View.GONE);
                } else {
                    if (mDataBinding.inputEditConfirmPasswd.getText().toString().length() > 0) {
                        mDataBinding.ivClearConfirmPasswdInput.setVisibility(View.VISIBLE);
                    }
                }
            }
        }
    };
    private TextWatcher mOldPassTextChangeListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //mDataBinding.inputEditOldPass.setSelection(s.length());
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mDataBinding.inputEditOldPass.isFocused()) {
                mDataBinding.ivClearOldPassInput.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
            }
            if(TextUtils.isEmpty(s)){
                mDataBinding.btnComleteInfo.setEnabled(false);
            }else {
                mDataBinding.btnComleteInfo.setEnabled(true);
            }
        }
    };
    private TextWatcher mPassTextChangeListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //mDataBinding.inputEditPasswd.setSelection(s.length());
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mDataBinding.inputEditPasswd.isFocused()) {
                mDataBinding.ivClearPasswdInput.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
            }
            if(TextUtils.isEmpty(s)){
                mDataBinding.btnComleteInfo.setEnabled(false);
            }else {
                mDataBinding.btnComleteInfo.setEnabled(true);
            }
        }
    };
    private TextWatcher mConfirmPassTextChangeListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //mDataBinding.inputEditConfirmPasswd.setSelection(s.length());
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mDataBinding.inputEditConfirmPasswd.isFocused()) {
                mDataBinding.ivClearConfirmPasswdInput.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
            }
            if(TextUtils.isEmpty(s)){
                mDataBinding.btnComleteInfo.setEnabled(false);
            }else {
                mDataBinding.btnComleteInfo.setEnabled(true);
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mDataBinding = FragmentOldPassChangeBinding.inflate(inflater, container, false);
        initViews();
        return mDataBinding.getRoot();
    }
    private void initViews() {
        mDataBinding.btnComleteInfo.setOnClickListener(createOnClickListener());
        mDataBinding.inputEditOldPass.addTextChangedListener(mOldPassTextChangeListener);
        mDataBinding.inputEditPasswd.addTextChangedListener(mPassTextChangeListener);
        mDataBinding.inputEditConfirmPasswd.addTextChangedListener(mConfirmPassTextChangeListener);
        mDataBinding.inputEditOldPass.setOnFocusChangeListener(mInputFocusListener);
        mDataBinding.inputEditPasswd.setOnFocusChangeListener(mInputFocusListener);
        mDataBinding.inputEditConfirmPasswd.setOnFocusChangeListener(mInputFocusListener);
        mDataBinding.ivClearOldPassInput.setOnClickListener(createOnClickListener());
        mDataBinding.ivClearPasswdInput.setOnClickListener(createOnClickListener());
        mDataBinding.ivClearConfirmPasswdInput.setOnClickListener(createOnClickListener());
    }
    private View.OnClickListener createOnClickListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UdaUtils.hideInputMethod(getContext(), mDataBinding.inputEditOldPass);
                if(R.id.btn_comlete_info == v.getId()) {
                    mDataBinding.ivClearOldPassInput.setVisibility(View.GONE);
                    mDataBinding.ivClearPasswdInput.setVisibility(View.GONE);
                    mDataBinding.ivClearConfirmPasswdInput.setVisibility(View.GONE);
                    String oldPass = mDataBinding.inputEditOldPass.getText().toString().trim();
                    String passwd = mDataBinding.inputEditPasswd.getText().toString().trim();
                    String confirmPasswd = mDataBinding.inputEditConfirmPasswd.getText().toString().trim();
                    if(TextUtils.isEmpty(oldPass)) {
                        ToastUtils.show(getContext(), getString(R.string.old_pwd_can_not_be_null));
                        return;
                    }
                    if(TextUtils.isEmpty(passwd)) {
                        ToastUtils.show(getContext(), getString(R.string.pwd_can_not_be_null));
                        return;
                    }
                    if(TextUtils.isEmpty(confirmPasswd)) {
                        ToastUtils.show(getContext(), getString(R.string.confirm_pwd_can_not_be_null));
                        return;
                    }

                    if(!TextUtils.equals(passwd, confirmPasswd)) {
                        ToastUtils.show(getContext(), getString(R.string.twice_pwd_not_equal));
                        return;
                    }
                    changePassWord(oldPass, passwd);
                } else if(R.id.iv_clear_old_pass_input == v.getId()) {
                    mDataBinding.inputEditOldPass.setText("");
                } else if(R.id.iv_clear_passwd_input == v.getId()) {
                    mDataBinding.inputEditPasswd.setText("");
                } else if(R.id.iv_clear_confirm_passwd_input == v.getId()) {
                    mDataBinding.inputEditConfirmPasswd.setText("");
                }
            }
        };
    }
    private void changePassWord(String old, String newPass) {
        showProgressDialog();
        onNewRequestCall(UserChangePassowrdAction.newInstance(getContext(), old, newPass).request(new AHttpService.IResCallback<PbBean.PbResUserChangePassword>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResUserChangePassword> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if(getView() != null) {
                    closeProgressDialog();
                    if(HttpHelper.showError(getContext(), resultCode, baseServerInfo, getString(R.string.error_net))) {
                        return;
                    }
                    PbBean.PbResUserChangePassword info = response.body();
                    EventBusUtil.post(new IndividualEvent(IndividualEvent.USER_PASSWD_EVENT));
                }
            }
        }));
    }
}
